const app = getApp()
import {
  actid,
  baseUrl
} from '../../utils/config'
import {
  Data_req
} from '../../utils/util'
Page({

  data: {
    addrArr: ['城关', '安宁', '和平', '白银', '金昌', '天水', '武威', '张掖', '平凉', '酒泉', '庆阳', '定西', '陇南', '临夏', '甘南'],
    addrNull: null,
    addrIndex: 0,
    addr: '',
    easyLogin: true,
    isLogin: false,
    isShow: true,
  },

  onLoad(option) {
    if (wx.getStorageSync('is_sub')) {
      wx.redirectTo({
        url: '../succe/succe',
      })
    }
    if (option.area) {
      app.globalData.tgarea = option.area
    }
  },
  onShow() {
    if (wx.getStorageSync('is_sub')) {
      wx.redirectTo({
        url: '../succe/succe',
      })
    }
  },
  closeShade() {
    this.setData({
      isShow: true,
      easyLogin: true
    })
  },
  bindAddrPickerChange(e) {
    this.setData({
      addrNull: '0',
      addrIndex: e.detail.value,
      addr: this.data.addrArr[e.detail.value]
    })
  },
  submit(e) {
    let that = this;
    if (that.data.addr == '') {
      wx.showToast({
        title: "选择所在城市",
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    }
    if (!that.data.isLogin) {
      that.setData({
        easyLogin: false,
        isShow: false
      })
    }
  },
  getPhoneNumber(e) {
    wx.showLoading({
      title: '登录中...'
    })
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      wx.cloud.callFunction({
        name: 'login',
        data: {
          id: e.detail.cloudID
        }
      }).then(res => {
        this.register(res.result.res.list[0].data.phoneNumber)
      }).catch(err => {
        wx.showToast({
          title: '授权失败',
          icon: 'none'
        })
        this.closeShade()
      })
    } else {
      wx.showToast({
        title: '授权失败',
        icon: 'none'
      })
      this.closeShade()
    }
  },
  register(phone) {
    Data_req({
      url: `${baseUrl}getphonestatus?actid=${actid}&callback=?`,
      data: {
        phone,
        sstime: new Date().getTime()
      }
    }).then(res => {
      const res_data = JSON.parse(res.data.substring(1, res.data.length - 1))
      if (res_data.status == 1) {
        wx.showToast({
          title: '您已预约',
          icon: 'none'
        })
        wx.setStorageSync('is_sub', true)
        wx.redirectTo({
          url: '../succe/succe'
        })
      } else {
        Data_req({
          url: `${baseUrl}register?actid=${actid}&callback=?`,
          data: {
            phone,
            tid: app.globalData.tgarea,
            name: 'xcx',
            city: this.data.addr,
            sstime: new Date().getTime()
          }
        }).then(res => {
          const res_data = JSON.parse(res.data.substring(1, res.data.length - 1))
          if (res_data.status == 1) {
            wx.showToast({
              title: '预约成功',
              icon: 'none'
            })
            wx.setStorageSync('is_sub', true)
            wx.redirectTo({
              url: '../succe/succe'
            })
          } else {
            wx.showToast({
              title: '预约失败',
              icon: 'none'
            })
            this.closeShade()
          }
        }).catch(err => {
          wx.showToast({
            title: '预约失败',
            icon: 'none'
          })
          this.closeShade()
        })
      }
    }).catch(err => {
      wx.showToast({
        title: '预约失败',
        icon: 'none'
      })
      this.closeShade()
    })
  },
  onShareTimeline() {
    return {
      title: "2021年甘肃公务员考试公告及职位表预约服务",
      query: `/pages/index/index?area=${app.globalData.tgarea}`,
      imageUrl: "http://gs.offcn.com/zg/WechatApplet/skggyy/share.png?" + Math.random()
    }
  },
  onShareAppMessage() {
    return {
      title: "2021年甘肃公务员考试公告及职位表预约服务",
      path: `/pages/index/index?area=${app.globalData.tgarea}`,
      imageUrl: "http://gs.offcn.com/zg/WechatApplet/skggyy/share.png?" + Math.random()
    }
  }
})